Python: Propagate user-supplied _meta from function_invocation_kwargs to MCP …#5159
Open
Serjbory wants to merge 2 commits intomicrosoft:mainfrom
Open
Python: Propagate user-supplied _meta from function_invocation_kwargs to MCP …#5159Serjbory wants to merge 2 commits intomicrosoft:mainfrom
Serjbory wants to merge 2 commits intomicrosoft:mainfrom
Conversation
…call_tool requests
eavanvalkenburg
approved these changes
Apr 8, 2026
Contributor
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
…call_tool requests (#5158)
Motivation and Context
MCP servers can accept a
_metafield alongside tool arguments incall_toolrequests, enabling clients to pass request-level metadata such as correlation IDs, user locale, session info, and other context that servers may use for debugging, distributed tracing, or request-specific behavior.Both OpenAI Agents SDK (PR #2375) and LangChain MCP Adapters (PR #468) have recently added
_metasupport, making this an expected capability across MCP client frameworks.Description
Propagate user-supplied
_metafromfunction_invocation_kwargsto MCPcall_toolrequests. The change is minimal (~4 lines inMCPTool.call_tool()):_metafrom incoming**kwargsviakwargs.pop("_meta", None)before filtering"_meta"to the existing filtered-kwargs exclusion set so it never leaks into tool arguments_inject_otel_into_mcp_meta(user_meta)instead of_inject_otel_into_mcp_meta()— user keys take precedence, OTel keys fill in non-conflicting slotsThis uses the established
function_invocation_kwargsmechanism — the same per-request injection pattern already used across 6+ samples for API keys, user IDs, and session metadata:No new public types, parameters, or constructor changes. Fully backward compatible — when
_metais not provided, behavior is identical to before.Files changed:
python/packages/core/agent_framework/_mcp.py— extract and forward_metaincall_tool()python/packages/core/tests/core/test_mcp.py— 9 new tests covering all edge casesdocs/specs/002-mcp-meta-support.md— feature specTest coverage:
_metaforwarded as protocol-levelmeta, excluded from toolarguments_metais absent_meta={}treated as no user metaFunctionToolwrapper (as created byload_tools())MCPStreamableHTTPTooloverride forwards_metatosuper().call_tool()header_providerwithout interferenceContribution Checklist